package com.warframe.message;

import com.sobte.cqp.jcq.entity.CoolQ;
import com.sobte.cqp.jcq.entity.Group;
import com.sobte.cqp.jcq.entity.Member;
import com.tools.StringContent;
import com.warframe.activity.Activity;
import com.warframe.alerts.Alerts;
import com.warframe.cycletime.CycleTime;
import com.warframe.entity.activity.Award;
import com.warframe.entity.message.Message;
import com.warframe.fissures.Fissures;
import com.warframe.invasion.Invasion;
import com.warframe.luckdraw.LuckDraw;
import com.warframe.market.riven.RivenMarket;
import com.warframe.market.warframe.WarframeMarket;
import com.warframe.mod.Mod;
import com.warframe.robot;
import com.warframe.sortie.Sortie;
import com.warframe.welcome.WelCome;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Executor implements Runnable {
	//单例
	private static Executor executor;

	private Executor() {
	}

	private static class SingletonHolder {
		private final static Executor instance = new Executor();
	}

	public static Executor getInstance() {
		return Executor.SingletonHolder.instance;
	}

	private static ExecuMsgQueue execuMsgQueue = ExecuMsgQueue.getInstance();
	private static Boolean quit = false;
	private static Integer alertScheduledPlan = 0;
	private static CoolQ CQ;
	private static ExecutorService executorService;
	private static String patternNumber = "\\d";
	private static Boolean activityFlag = false;
	public static List<Award> activityAward = null;

	public static CoolQ getCQ() {
		return CQ;
	}

	public static void setCQ(CoolQ CQ) {
		Executor.CQ = CQ;
	}

	public void quit() {
		quit = true;
		executorService.shutdown();
	}

	private void init() {
		List<Group> groupIDList = CQ.getGroupList();
		for (Group eachGroup : groupIDList) {
			List<Member> groupMemberList = CQ.getGroupMemberList(eachGroup.getId());
			Map<Long, Member> groupMemberMap = new HashMap<>();
			for (Member eachMember : groupMemberList) {
				groupMemberMap.put(eachMember.getQqId(), eachMember);
			}
			StringContent.GROUPMEMBERLIST.put(eachGroup.getId(), groupMemberMap);
		}
	}

	private void openActivity(Message message) {
		try {
			activityAward = new Activity().initAwardList();
		} catch (Exception e) {
			activityAward = null;
			activityFlag = false;
			throw new RuntimeException("活动开启失败", e);
		}
		activityFlag = true;
		message.setMsg("活动开启成功");
		message.setSign(StringContent.SENDPERSON);
		this.pushMsg(message);
	}

	private void closeActivity(Message message) {
		activityFlag = false;
		activityAward = null;
		List<String> result = new ArrayList<>();
		message.setMsg("");
		for (String value : StringContent.AWARDMAP.values()) {
//			result.add(value);
			message.setMsg(message.getMsg() + value);
		}
		message.setMsg("活动关闭成功");
		message.setSign(StringContent.SENDPERSON);
		this.pushMsg(message);
	}

	//Main
	@Override
	public void run() {
		this.init();
		CQ.logInfo("WarframeRobot", "Executor Init Finished");
		executorService = Executors.newScheduledThreadPool(3);
		while (!quit) {
			if (!execuMsgQueue.isEmpty()) {
				Message message = execuMsgQueue.poll();
				switch (message.getSign()) {
					case StringContent.SENDGROUP:
						CQ.sendGroupMsg(message.getGroupId(), message.getMsg());
						break;
					case StringContent.SENDPERSON:
						CQ.sendPrivateMsg(message.getFromQQ(), message.getMsg());
						break;
//					case StringContent.ACTIVITY:
//						this.executorActivity(message);
//						break;
					case StringContent.NEWJOIN:
						this.executorWelcome(message);
						break;
					case StringContent.LUCKDRAW:
						this.executorLuckDraw(message);
						break;
					case StringContent.WARFRAMEMARKET:
						this.executorWarframeMarket(message);
						break;
					case StringContent.ALERT:
						this.executorAlerts(message);
						break;
					case StringContent.RIVENMARKET:
						this.executorRivenMarket(message);
						break;
					case StringContent.CETUSTIME:
						this.executorCetusTime(message);
						break;
					case StringContent.MOD:
						this.executorMod(message);
						break;
					case StringContent.INVASION:
						this.executorInvasion(message);
						break;
					case StringContent.SORTIE:
						this.executorSortie(message);
						break;
					case StringContent.FISSURES:
						this.executorFissures(message);
						break;
					case StringContent.OPENACTIVITY:
						if (!activityFlag &&
								(message.getFromQQ().equals(454912294L)
										|| message.getFromQQ().equals(620017713L)
										|| message.getFromQQ().equals(576686714L))) {
							this.openActivity(message);
						}
						break;
					case StringContent.CLOSEACTIVITY:
						if (activityFlag &&
								(message.getFromQQ().equals(454912294L)
										|| message.getFromQQ().equals(620017713L)
										|| message.getFromQQ().equals(576686714L))) {//管理员写死，
							this.closeActivity(message);
						}
						break;
//                    case StringContent.LAYERCOMMAND:
//                        this.
//                        break;
				}
			} else {
				try {
					Thread.sleep(100);
//                    alertScheduledPlan++;
//                    if (alertScheduledPlan == 1200) {
//                        Alerts alerts = new Alerts(true);
//                        executorService.execute(alerts);
//                        alertScheduledPlan = 0;
//                    }
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void pushMsg(Message msg) {
		execuMsgQueue.add(msg);
	}

	private void executorRivenMarket(Message message) {
		RivenMarket rivenMarket = new RivenMarket(message);
		executorService.execute(rivenMarket);
	}

	private void executorWarframeMarket(Message message) {
		WarframeMarket warframeMarket = new WarframeMarket(message);
		executorService.execute(warframeMarket);
	}

	private void executorCetusTime(Message message) {
		CycleTime cycleTime = new CycleTime(message);
		executorService.execute(cycleTime);
	}

	private void executorAlerts(Message message) {
		Alerts alerts = new Alerts(message);
		executorService.execute(alerts);
	}

	private void executorWelcome(Message message) {
		WelCome welCome = new WelCome(message);
		executorService.execute(welCome);
	}

	private void executorMod(Message message) {
		Mod mod = new Mod(message);
		executorService.execute(mod);
	}

	private void executorInvasion(Message message) {
		Invasion invasion = new Invasion(message);
		executorService.execute(invasion);
	}

	private void executorActivity(Message message) {
		Activity activity = new Activity(message);
		executorService.execute(activity);
	}

	private void executorLuckDraw(Message message) {
		if (activityFlag
				&& StringContent.AWARDMAP.get(message.getFromQQ()) == null
				&& message.getGroupId().equals(553833612L)) {//测试结束后干掉
			this.executorActivity(message);
		} else {
			LuckDraw luckDraw = new LuckDraw(message);
			executorService.execute(luckDraw);
		}
	}

	private void executorFissures(Message message) {
		Fissures fissures = new Fissures(message);
		executorService.execute(fissures);
	}

	private void executorSortie(Message message) {
		Sortie sortie = new Sortie(message);
		executorService.execute(sortie);
	}
}
